version 12.0
set more off
capture log close
log using schneiderrohlfing_smr_simul.log, replace

* Do-file producing formula scores for hypothetical data, mentioned in footnote 41 in
* Schneider, Carsten Q. and Ingo Rohlfing (2014): 
* Case Studies Nested in Fuzzy-Set Qca on Sufficiency: Formalizing Case Selection and Causal Inference
* Sociological Methods & Research advance access: dx.doi.org/10.1177/0049124114532446
* Do-file 
* If you have any questions or comments, please get in touch with:
* Carsten Schneider (schneiderc@ceu.hu) or Ingo Rohlfing (rohlfing@bigsss.uni-bremen.de)

* Creating simulated data
clear
set obs 21
egen suff = fill(0(0.05)1)
replace suff = round(suff,.01)
label var suff "Sufficient condition"
save suff, replace
rename suff out
label var out "Outcome"
save out, replace
use suff, clear
cross using out
label data "Simulated data for single-case selection"
save suff_simul_single, replace

* Generating secondary diagonal for xy plots
gen sd = (_n-1)/10
replace sd = . if sd > 1
label var sd "Secondary diagonal"

*******************************	
* Formulas for case selection *
*******************************

* Typical case and deviant case coverage (formulas 1 and 3 in manuscript)
gen typ_dcov = .
replace typ_dcov = (out-suff)/suff ///
	if suff > 0.5 & out > 0.5 & suff <= out 
label var typ "Formula score for typical case and deviant case coverage"
replace typ_dcov = round(typ_dcov, .01)

* Note: The same formula applies for typical cases and deviant cases coverage.
* The X in the plot thus can stand for membership of a typical case in a 
* sufficient term of the solution or membership of a deviant case coverage
* in a truth table row.

* Plot of formula scores
scatter out suff, ///
	mlab(typ_dcov) ms(i) mlabcolor(gs0) mlabsize(vsmall) mlabpos(0) || ///
scatter sd sd , ms(i) c(l) lc(gs8) lw(vthin) ///
	xline(0.5, lc(gs8) lp(dash_dot)) xlabel(0(0.1)1, labsize(small)) ///
	yline(0.5, lc(gs8) lp(dash_dot)) ylabel(0(0.1)1, labsize(small)) ///
	text(0.54 0.515 "{it:1}", size(small)) text(0.54 0.56 "{it:2}", size(small)) ///
	text(0.47 0.515 "{it:3}", size(small)) text(0.47 0.485 "{it:4}", size(small)) ///
	text(0.47 0.44 "{it:4}", size(small)) text(0.54 0.485 "{it:5}", size(small)) ///
	xtitle("Membership in sufficient condition {it:X}", size(medsmall)) ///
	ytitle("Membership in outcome {it:Y}", size(medsmall)) ///
	title("Formula scores for typical case and deviant case coverage", size(medsmall)) ///
	scheme(s1color) legend(off)
graph save typ_dcov.gph, replace
graph export typ_dcov.emf, replace

* Deviant case for consistency in kind (formula 2 in manuscript)
gen dconsk = .
replace dconsk = (1-(suff-out))/suff ///
	if suff > 0.5 & out < 0.5
label var dconsk "Formula for deviance consistency in kind"
replace dconsk = round(dconsk, .01)

scatter out suff, ///
	mlab(dconsk) ms(i) mlabcolor(gs0) mlabsize(vsmall) mlabpos(0) || ///
scatter sd sd , ms(i) c(l) lc(gs8) lw(vthin) ///
	xline(0.5, lc(gs8) lp(dash_dot)) xlabel(0(0.1)1, labsize(small)) ///
	yline(0.5, lc(gs8) lp(dash_dot)) ylabel(0(0.1)1, labsize(small)) ///
	text(0.54 0.515 "{it:1}", size(small)) text(0.54 0.56 "{it:2}", size(small)) ///
	text(0.47 0.515 "{it:3}", size(small)) text(0.47 0.485 "{it:4}", size(small)) ///
	text(0.47 0.44 "{it:4}", size(small)) text(0.54 0.485 "{it:5}", size(small)) ///
	xtitle("Membership in sufficient condition {it:X}", size(medsmall)) ///
	ytitle("Membership in outcome {it:Y}", size(medsmall)) ///
	title("Formula scores for deviant case consistency in kind", size(medsmall)) ///
	scheme(s1color) legend(off)
graph save dconsk.gph
graph export dconsk.emf

log close
